import { EventData } from "./datas/EventData";
import { GameConst } from "./GameConst";
import { GameBlockData } from "./GameDefine";
import { EventMgr } from "./managers/EventMgr";
import { ObjectUtils } from "./utils/ObjectUtils";

/**
 * 游戏地图块
 * @Author: lzmxqh  
 * @Date: 2022-10-17 14:17:12  
 * @Last Modified by: lzmxqh
 * @Last Modified time: 2022-10-23 23:50:06
 */
export class GameBlock extends Phaser.GameObjects.Container {
    /**地图块数据 */
    private blockData: GameBlockData;

    /**地块背景 */
    private bg: Phaser.GameObjects.Arc;

    constructor(scene: Phaser.Scene, x: number, y: number, blockData: GameBlockData) {
        super(scene, x, y, []);
        const t = this;
        scene.add.existing(t);

        t.blockData = blockData;
        t.initUI();
    }

    /**获取地图块数据 */
    public getBlockData(): GameBlockData {
        return ObjectUtils.deepCopy(this.blockData);
    }

    /**初始化UI */
    private initUI(): void {
        const t = this;
        const bg: Phaser.GameObjects.Arc = t.scene.add.circle(0, 0, GameConst.RADIUS, 0xb3d9ff);
        t.add(bg);

        let shape: Phaser.Geom.Circle = new Phaser.Geom.Circle(GameConst.RADIUS, GameConst.RADIUS, GameConst.RADIUS);
        bg.setInteractive(shape, Phaser.Geom.Circle.Contains).on("pointerup", t.onClick, t);
        t.bg = bg;
    }

    /**重置数据 */
    public reset(): void {
        this.changeState(false);
    }

    /**改变状态 */
    public changeState(isClick: boolean): void {
        if (isClick) {
            this.bg.fillColor = 0x003366;
            this.blockData.isClick = true;
        } else {
            this.bg.fillColor = 0xb3d9ff;
            this.blockData.isClick = false;
        }
    }

    private onClick(): void {
        if (this.blockData.isClick) {
            // 发送点击地块事件
            EventMgr.getInst().emit(EventData.CLICK_BLOCK);
            return;
        }
        // 发送点击地块事件
        EventMgr.getInst().emit(EventData.CLICK_BLOCK, this.blockData);
    }
}